/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package com.desire.homework;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

/**
 * <p>Project: app2022 - _44_写字体
 * <p>Powered by webrx On 2022-01-21 11:45:13
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class _44_写字体 {
    public static void main(String[] args) {
        var w = 800;
        var h = 800;
        var t = 2;
        var i = new BufferedImage(w, h, t);
        var g = i.createGraphics();
        //设置线条和文件的，抗锯齿（平滑）
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
        //背景颜色效果
        var rand = new Random();
        var bgc = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
        g.setColor(bgc);
        g.fillRect(0, 0, w, h);


        g.setColor(Color.RED);
        g.drawLine(0, h / 2, w, h / 2);
        g.drawLine(w / 2, 0, w / 2, h);

        //文本字符串
        String text = "北游教育 - 版权所有";

        g.setColor(new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256), 255));

        //系统字体
        var font = new Font("微软雅黑", Font.BOLD, 50);


        //加载外部字体文件/src/main/resources/fonts/f1.ttf  wrjls.ttf
        var is = _44_写字体.class.getClassLoader().getResourceAsStream("fonts/f1.ttf");
        var u = _44_写字体.class.getClassLoader().getResource("fonts/f1.ttf");
        var file = new File(u.getPath());

        //加载外部字体/src/main/resources/fonts/f1.ttf  wrjls.ttf
        //var is2 = Thread.currentThread().getContextClassLoader().getResourceAsStream("fonts/f1.ttf");
        try {
            font = Font.createFont(Font.TRUETYPE_FONT, is);
            //font = Font.createFont(0, file);
            System.out.println(font.getName());
        } catch (FontFormatException | IOException e) {
            e.printStackTrace();
        }


        font = font.deriveFont(30f);

        //获取字符串的宽和高
        var fm = g.getFontMetrics(font);
        int tw = fm.stringWidth(text);
        //int th = fm.getHeight() 文本高度有误差
        //System.out.println(tw);
        //System.out.println(fm.getHeight());
        //System.out.println(fm.getAscent());
        //System.out.println(fm.getDescent());


        g.setFont(font);
        //左上角
        int sp = 10;
        int x = sp + 0;
        int y = sp + fm.getAscent() - fm.getDescent();

        //右下角
        x = w - tw - sp;
        y = h - sp - fm.getDescent();

        //正中央
        x = w / 2 - tw / 2;
        y = h / 2 + (fm.getAscent() - fm.getDescent()) / 2;

        //底部水平中央
        x = w / 2 - tw / 2;
        y = h - sp - fm.getDescent();

        //右上角
        x = w - tw - sp;
        y = sp + fm.getAscent() - fm.getDescent();

        g.drawString(text, x, y);


        g.dispose();
        //保存图像文件
        try {
            ImageIO.write(i, "png", new File("i.png"));
        } catch (Exception e) {
            e.printStackTrace();
        }

        //打开图像文件
        try {
            Runtime.getRuntime().exec("cmd /k start i.png");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
